//
// Created by 吴喆 on 2020/3/26.
//

#include "DemoMonster.h"

DemoMonster::DemoMonster(const Scene *scene, DIRECTION direction) : NPC(scene, direction) {
    DemoMonster::init();
}

void DemoMonster::initNPCStateMachine() {
    machines.emplace_back(new DemoMonsterStateMachine);
}

void DemoMonster::render() {
    Texture2D *currTexture = Playable::currentAnimation->updateAndGetCurrentFrame();
    if (isAtked) {

        renderer->DrawSprite(scene->getCamera(), currTexture, GameObject::currPosition,
                            GameObject::size, 0.0f,
                            Playable::direction == DIRECTION::LEFT, false, true, glm::vec4(1));

        interval += CTime::getInstance()->getDelta();
        if (AtkedStateInterval <= interval) {
            isAtked = false;
            interval = 0;
        }
    } else {
        renderer->DrawSprite(scene->getCamera(), currTexture, GameObject::currPosition,
                            GameObject::size, 0.0f,
                            Playable::direction == DIRECTION::LEFT);
    }
}

void DemoMonster::initGameObject() {
    GameObject::initGameObject();
    GameObject::size = glm::vec2(4, 4);
    GameObject::initPosition = glm::vec2(3, 3);
}

void DemoMonster::init() {
    NPC::init();
}

void DemoMonster::beAtked(float val) {
    isAtked = true;
    interval = 0;
}

void DemoMonster::initSensorField() {
    b2BodyDef bodyDef;
    bodyDef.type = b2_kinematicBody;
    bodyDef.position.Set(body.startPos.x, GameObject::size.y / 4 + body.startPos.y);
    body.sensor = scene->world->createBody(&bodyDef);
    body.sensor->SetFixedRotation(true);
    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(GameObject::size.x / 6, GameObject::size.y / 4);
    body.fixtureDef.isSensor = true;
    b2FixtureDef fixtureDef = body.fixtureDef;
    fixtureDef.shape = &dynamicBox;
    // Add the shape to the body.
    body.sensor->CreateFixture(&fixtureDef);
    body.sensor->SetUserData(this);
}


